package com.netty.bfport.netty.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhaoyang05
 * @Data: 2022/3/1 11:12
 * @Description
 * 处理器加载类
 */
@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    ServerIdleStateTrigger serverIdleStateTrigger;
//    @Resource
//    HeartHandler heartHandler;
//    @Resource
//    LineBasedFrameDecoder lineBasedFrameDecoder;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //添加编解码
        socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast(new IdleStateHandler(10,5,7, TimeUnit.SECONDS));

        socketChannel.pipeline().addLast(serverIdleStateTrigger);
        socketChannel.pipeline().addLast(serverIdleStateTrigger);

        socketChannel.pipeline().addLast(new StringDecoder());
        socketChannel.pipeline().addLast(nettyServerHandler);

//        socketChannel.pipeline().addLast(heartHandler);
    }
}
